package com.sfy.mallchat.common.user.controller;

import com.sfy.mallchat.common.common.domain.dto.RequestInfo;
import com.sfy.mallchat.common.common.utils.AssertUtil;
import com.sfy.mallchat.common.common.utils.RequestHolder;
import com.sfy.mallchat.common.user.domain.enums.RoleEnum;
import com.sfy.mallchat.common.user.domain.vo.req.ModifyNameReq;
import com.sfy.mallchat.common.user.domain.vo.resp.BadgeResp;
import com.sfy.mallchat.common.user.domain.vo.resp.BlackReq;
import com.sfy.mallchat.common.user.domain.vo.resp.UserInfoResp;
import com.sfy.mallchat.common.user.domain.vo.resp.WearingBadgeReq;
import com.sfy.mallchat.common.user.service.IRoleService;
import com.sfy.mallchat.common.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.sfy.mallchat.common.common.domain.ApiResult;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/capi/user")
@Api(tags = "用户管理")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private IRoleService roleService;

    @ApiOperation("用户信息服务")
    @GetMapping("public/userInfo")
    public ApiResult<UserInfoResp> getUserInfo(){
       return ApiResult.success(userService.getUserInfo(RequestHolder.get().getUid()));
    }

    @ApiOperation("用户信息服务")
    @GetMapping("public/name")
    public ApiResult<Void> modifyName(@RequestBody @Validated ModifyNameReq modifyNameReq){
        userService.modifyName(RequestHolder.get().getUid(),modifyNameReq.getName());
        return ApiResult.success();
    }


    @ApiOperation("用户徽章列表")
    @GetMapping("public/badges")
    public ApiResult<List<BadgeResp>> badges(){
       List<BadgeResp> resList = userService.badeges(RequestHolder.get().getUid());
       return ApiResult.success(resList);
    }

    @ApiOperation("佩戴徽章")
    @PostMapping("public/badges")
    public ApiResult<Void> wearingBadge(@RequestBody @Validated WearingBadgeReq req){
        userService.wearingBadge(RequestHolder.get().getUid(),req);
        return ApiResult.success();
    }


    @ApiOperation("黑名单")
    @PostMapping("public/black")
    public ApiResult<Void> black(@RequestBody @Validated BlackReq req){
        Long uid = RequestHolder.get().getUid();
        boolean hasPermission = roleService.hasPermission(uid, RoleEnum.ADMIN);
        AssertUtil.isTrue(hasPermission, "没有用户该权限");
        userService.black(req);
        return ApiResult.success();
    }
}

